//
//  YREmotionToolbar.swift
//  新浪微博
//
//  Created by Viper on 16/7/4.
//  Copyright © 2016年 Viper. All rights reserved.
//

import UIKit

private let tagNumber = 1000

//表情键盘类型枚举
enum YREmotionToolbarType : Int {
    case DefaultButton = 0
    case EmojiButton = 1
    case XlhButton = 2
}
class YREmotionToolbar: UIView {
    //声明闭包 告诉外界哪个按钮点击
    var toolbarClosure:((type :YREmotionToolbarType )->())?
    
    var currentButton : UIButton?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 供外界调用方法
    func setCurrentButtons(tag : Int){
        
        let button = viewWithTag(tag + tagNumber) as! UIButton
        
        //如果当前button等于选中的button直接返回
        if currentButton == button{
            return
        }
        
        /*
         -传入的button变为选中状态
         -保存的button状态变为正常状态
         -保存的button == 传入的button
         */
        
        button.selected = true
        
        currentButton?.selected = false
        
        currentButton = button
        

    
    }
    
    //MARK: - 设置视图
    private func setupUI(){
        
//        backgroundColor = RandomColor()
  
        let defaultButton = addChildButton("left", title: "默认",type: .DefaultButton)
        let emojiButton = addChildButton("mid", title: "Emoji",type: .EmojiButton)
        let xlhButton =  addChildButton("right", title: "小浪花",type: .XlhButton)
        defaultButton.snp_makeConstraints { (make) in
            
            make.top.leading.bottom.equalTo(self)
            make.width.equalTo(emojiButton)
            
        }
        emojiButton.snp_makeConstraints { (make) in
            
            make.width.equalTo(xlhButton)
            make.top.bottom.equalTo(self)
            make.leading.equalTo(defaultButton.snp_trailing)
            
            
        }
        xlhButton.snp_makeConstraints { (make) in
            
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(emojiButton.snp_trailing)
        }
        
//        //首次默认按钮为选中状态
        defaultButton.selected = true
        currentButton = defaultButton
//
    }
    //MARK: - 监听方法
    func buttonClick(button : UIButton){
        //如果当前button等于选中的button直接返回
        if currentButton == button{
            return
        }
        
        /*
         -传入的button变为选中状态
         -保存的button状态变为正常状态
         -保存的button == 传入的button
         */
        
        button.selected = true
        
        currentButton?.selected = false
        
        currentButton = button
        
        //调用闭包
        toolbarClosure?(type :YREmotionToolbarType (rawValue:button.tag - tagNumber)!)
//        print("点击了")
        
    }
    
    
    //创建button的公共方法
    
    func addChildButton(imageNamed : String,title : String,type : YREmotionToolbarType)->UIButton{
       
        let button = UIButton()
        button.tag = type.rawValue + tagNumber
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imageNamed)_normal"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imageNamed)_selected"), forState: UIControlState.Selected)
        button.titleLabel?.font = UIFont.systemFontOfSize(16)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Selected)
        button.addTarget(self, action: #selector(YREmotionToolbar.buttonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        button.setTitle(title, forState: UIControlState.Normal)
        button.setTitle(title, forState: UIControlState.Selected)
        addSubview(button)
        
        return button
    }
    
    
}
